package com.ruoyi.nongjt.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.nongjt.domain.NjtUserRegister;
import com.ruoyi.nongjt.domain.User;
import com.ruoyi.nongjt.mapper.NjtUserRegisterMapper;
import com.ruoyi.nongjt.service.NjtUserRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class NjtUserRegisterServiceImpl implements NjtUserRegisterService {
    @Autowired
    private NjtUserRegisterMapper njtUserRegisterMapper;

    @Override
    public List<NjtUserRegister> getNjtUserRegisterList(NjtUserRegister njtUserRegister) {
        return njtUserRegisterMapper.selectNjtUserRegisterList(njtUserRegister);
    }

    @Override
    public int updateNjtUserRegister(NjtUserRegister njtUserRegister) {
        njtUserRegister.setUpdateBy(SecurityUtils.getUsername());
        njtUserRegister.setUpdateTime(new Date());
        return njtUserRegisterMapper.updateNjtUserRegister(njtUserRegister);
    }

    @Override
    public int deleteNjtUserRegisterById(Integer userId) {
        return njtUserRegisterMapper.deleteNjtUserRegisterById(userId);
    }


    @Override
    public User findByPhone(String phone) {
        return njtUserRegisterMapper.findByPhone(phone);
    }

    @Override
    public boolean register(User user) {
        return njtUserRegisterMapper.register(user) > 0;
    }

    @Override
    public User login(String phone, String password) {
        User user = njtUserRegisterMapper.findByPhone(phone);
        if (user != null && SecurityUtils.matchesPassword(password, user.getPassword())) {
            return user;
        }
        return null;
    }
    @Override
    public int deleteNjtUserRegisterByIds(Long[] userIds) {
        return njtUserRegisterMapper.deleteNjtUserRegisterByIds(userIds);
    }
}
